//
//  ShopDetailTableViewCell.m
//  FreeTrip
//
//  Created by 352324497@qq.com on 15/11/25.
//  Copyright © 2015年 邓芮. All rights reserved.
//

#import "ShopDetailTableViewCell.h"

@implementation ShopDetailTableViewCell
{
    CGFloat labelHeight;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self.selectionStyle = NO;
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.selectionStyle = NO;
        CGFloat width = KScreenWidth - 20 * KScreenWidth / 375;
        self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10 * KScreenWidth / 375, 10 * KScreenWidth / 375, width, 30 * KScreenWidth / 375)];
        self.titleLabel.font = [UIFont boldSystemFontOfSize:16 * KScreenWidth / 375];
        [self.contentView addSubview:_titleLabel];
        
        self.myDescriptionLabel = [[UILabel alloc]initWithFrame:CGRectMake(10 * KScreenWidth / 375, 50 * KScreenWidth / 375, width, 10 * KScreenWidth / 375)];
        self.myDescriptionLabel.font = [UIFont  systemFontOfSize:16 * KScreenWidth / 375];
        self.myDescriptionLabel.numberOfLines = 0;
        self.myDescriptionLabel.textColor = [UIColor grayColor];
        [self.contentView addSubview:_myDescriptionLabel];
    }
    return self;
}

-(void)setModel:(ShopDetailModel *)model
{
    _model = model;
    self.titleLabel.text = model.title;
    self.myDescriptionLabel.text = model.myDescription;
    // 创建实例, 将text 传入
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:self.myDescriptionLabel.text];
    // 创建实例
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
    // 设置行间距
    [style setLineSpacing:10];
    // 判断内容长度是否大于 Label 内容的宽度, 如果不大于, 则设置内容宽度为行宽 (内容如果小于行宽, Label 长度太短, 在加背景颜色的情况下会有影响)
    CGFloat wid = self.myDescriptionLabel.frame.size.width;
    if (attStr.length < self.myDescriptionLabel.frame.size.width)
    {
        wid = attStr.length;
    }
    // 根据给定的长度与 style 设置 attStr 样式
    [attStr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, wid)];
    // Label 获取样式
    self.myDescriptionLabel.attributedText =attStr;
    //  Label 自适应大小
    [self.myDescriptionLabel sizeToFit];
//    labelHeight = self.myDescriptionLabel.frame.size.height;
    
}
+(CGFloat)heightForCell:(ShopDetailModel *)model
{
    CGFloat width = KScreenWidth - 20 * KScreenWidth / 375;
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10 * KScreenWidth / 375, 50 * KScreenWidth / 375, width, 10 * KScreenWidth / 375)];
    label.numberOfLines = 0;
    label.font = [UIFont  systemFontOfSize:16 * KScreenWidth / 375];
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:model.myDescription];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
    [style setLineSpacing:10];
    CGFloat wid = width;
    if (attStr.length < width)
    {
        wid = attStr.length;
    }
    [attStr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, wid)];
    label.attributedText =attStr;
    [label sizeToFit];
    CGFloat height = label.frame.size.height;
    return height + 60 * KScreenWidth / 375;
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
